package org.btik.redstone.led;


import org.btik.redstone.led.conf.Conf;
import org.btik.redstone.led.listener.RedStoneListener;
import org.btik.redstone.led.sender.LightRawNbi;
import org.bukkit.Server;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;

public final class RedstoneLed extends JavaPlugin {

    private LightRawNbi lightRawNbi;

    @Override
    public void onEnable() {
        Logger logger = getSLF4JLogger();

        Conf conf = loadConf(logger);
        logger.info(conf.toString());

        lightRawNbi = new LightRawNbi(conf, logger);
        lightRawNbi.start();
        Server server = getServer();
        PluginManager pluginManager = server.getPluginManager();
        pluginManager.registerEvents(new RedStoneListener(lightRawNbi, logger), this);

    }

    private Conf loadConf(Logger logger) {
        File dataFolder = getDataFolder();
        if (!dataFolder.exists()) {
            if (!dataFolder.mkdirs()) {
                logger.error("cannot create RedstoneLed folder ");
                throw new RuntimeException("cannot create RedstoneLed folder");
            }
        }
        try {
            return new Conf(dataFolder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onDisable() {
        lightRawNbi.shutDown();
    }
}
